﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDD;
using Xunit;

namespace DokiDokiTest {
   public  class TestSampler {

       [Fact]
       public void Test_New1 () {
           var tex = new Sampler (SamplerType.Texture2D);

           Assert.Equal (SamplerType.Texture2D, tex.Type);
           Assert.Equal (1, tex.ImageCount);
           Assert.Equal (null, tex.Image);
           Assert.Equal (ImageFilter.Linear, tex.ImageFilter);
           Assert.Equal (LevelFilter.Linear, tex.LevelFilter);
           Assert.Equal (TextureWrapMode.Repeat, tex.WrapMode);
       }

       [Fact]
       public void Test_SetImage_1 () {
           var smp = new Sampler (SamplerType.Texture2D);
           var img = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
           smp.SetImage (img);

           Assert.Equal (1, smp.ImageCount);
           Assert.Equal (img, smp.Image);
       }

       [Fact]
       public void Test_SetImage_2 () {
           var sampler = new Sampler (SamplerType.TextureCube);
           var negX = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
           var posX = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
           var negY = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
           var posY = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
           var negZ = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
           var posZ = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);

           sampler.SetImage (CubeFace.NegativeX, negX);
           sampler.SetImage (CubeFace.PositiveX, posX);
           sampler.SetImage (CubeFace.NegativeY, negY);
           sampler.SetImage (CubeFace.PositiveY, posY);
           sampler.SetImage (CubeFace.NegativeZ, negZ);
           sampler.SetImage (CubeFace.PositiveZ, posZ);

           Assert.Equal (6, sampler.ImageCount);
           Assert.Equal (negX, sampler.GetImage (CubeFace.NegativeX));
           Assert.Equal (posX, sampler.GetImage (CubeFace.PositiveX));
           Assert.Equal (negY, sampler.GetImage (CubeFace.NegativeY));
           Assert.Equal (posY, sampler.GetImage (CubeFace.PositiveY));
           Assert.Equal (negZ, sampler.GetImage (CubeFace.NegativeZ));
           Assert.Equal (posZ, sampler.GetImage (CubeFace.PositiveZ));
       }


       [Fact]
       public void Test_SetImages () {
           var samp = new Sampler (SamplerType.TextureCube);
           var imgs = new Image2D<byte>[6]{ new Image2D<byte>(PixelFormat.RGB8ui, 16, 16),
                                            new Image2D<byte>(PixelFormat.RGB8ui, 16, 16),
                                            new Image2D<byte>(PixelFormat.RGB8ui, 16, 16),
                                            new Image2D<byte>(PixelFormat.RGB8ui, 16, 16),
                                            new Image2D<byte>(PixelFormat.RGB8ui, 16, 16),
                                            new Image2D<byte>(PixelFormat.RGB8ui, 16, 16)};
           samp.SetImages (imgs);

           Assert.Equal (6, samp.ImageCount);
           Assert.Equal (imgs, samp.Images);
       }



       [Fact]
       public void Test_Eumerator () {
           var imgs = new Image2D[]{new Image2D<byte>(PixelFormat.RGB8ui, 16,16),
                                     new Image2D<byte>(PixelFormat.RGB8ui, 16,16),
                                     new Image2D<byte>(PixelFormat.RGB8ui, 16,16),
                                     null, 
                                     new Image2D<byte>(PixelFormat.RGB8ui, 16,16),
                                     new Image2D<byte>(PixelFormat.RGB8ui, 16,16)};
           var tex = new Sampler (SamplerType.TextureCube);
           tex.SetImages (imgs);

           var sum = tex.Images.Sum (x=> 1);

           Assert.Equal (5, sum);
       }

       [Fact]
       public void Test_SetFilter () {
           var tex = new Sampler (SamplerType.Texture2D);
           
           tex.SetFilter (ImageFilter.Nearest, LevelFilter.Base);

           Assert.Equal (ImageFilter.Nearest, tex.ImageFilter);
           Assert.Equal (LevelFilter.Base, tex.LevelFilter);
       }

       [Fact]
       public void Test_SetWrapMode () {
           var tex = new Sampler (SamplerType.Texture2D);

           tex.SetWrapMode (TextureWrapMode.Clamp);

           Assert.Equal (TextureWrapMode.Clamp, tex.WrapMode);
       }

       [Fact]
       public void Test_References () {
           var img = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
           var tex = new Sampler (SamplerType.Texture2D);
           tex.SetImage (img);

           var objs = tex.References;

           Assert.Equal (1, objs.Count ());
           Assert.Equal (true, objs.All (x => x != null));
       }

       [Fact]
       public void Test_Find () {
           var img = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
           var tex = new Sampler (SamplerType.Texture2D);
           tex.SetImage (img);

           img.UserID = 101;
           tex.UserID = 102;

           Assert.Equal (img, tex.Find (101));
           Assert.Equal (tex, tex.Find (102));
       }
   }
}
